package com.xiaoyu.lanling.feature.vip.model

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.views.list.ListItemTypedBase
import android.graphics.Color
import android.text.TextUtils
import java.io.Serializable

class ProductItem(jsonData: JsonData, val vipInfo: VipInfo) : ListItemTypedBase, Serializable {

    val id = jsonData.optString("id")
    val price = jsonData.optString("price")
    val duration = jsonData.optString("duration")
    val subject = jsonData.optString("subject")
    val vipTag = VipTag(jsonData.optJson("vipTag"))
    val highlight = jsonData.optBoolean("highlight")

    override fun getViewType(): Int {
        return 0
    }

    inner class VipTag(jsonData: JsonData) : Serializable {
        val desc = jsonData.optString("tagDesc")
        val color = parseColor(jsonData.optString("tagColor"))
        val valid = !TextUtils.isEmpty(desc) && color != 0

        private fun parseColor(color: String): Int {
            if (color.isEmpty()) {
                return 0
            }
            return try {
                Color.parseColor(color)
            } catch (e: Exception) {
                e.printStackTrace()
                0
            }
        }
    }
}